Part 3

Row

Accidents Map

Accidents by Gender

Death Rate by User Type

Roads with most accidents and deaths

Accidents by road
Road Accidents
PRINCES HIGHWAY 3581
HIGH STREET 3096
NEPEAN HIGHWAY 2376
SPRINGVALE ROAD 1663
SOUTH GIPPSLAND HIGHWAY 1538
SYDNEY ROAD 1538
MONASH FREEWAY 1533
MAROONDAH HIGHWAY 1335
Deadliest roads
Road Accidents Deaths Deaths_per_accident
GLENELG HIGHWAY 231 28 0.1212121
GOULBURN VALLEY HIGHWAY 344 39 0.1133721
WIMMERA HIGHWAY 156 17 0.1089744
MURRAY VALLEY HIGHWAY 727 76 0.1045392
STRZELECKI HIGHWAY 106 11 0.1037736
HAMILTON HIGHWAY 223 21 0.0941704
MELBA HIGHWAY 211 18 0.0853081
NORTHERN HIGHWAY 300 25 0.0833333
---
title: "Motor Vehicle Accidents in Victoria"
output: 
  flexdashboard::flex_dashboard:
        vertical_layout: scroll
        orientation: rows
        source_code: embed
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
                      eval = TRUE,
                      message = FALSE, 
                      warning = FALSE)
```

```{r libraries}
library(flexdashboard)
library(tidyverse)
library(lubridate)
library(janitor)
library(knitr)
library(kableExtra)
library(ggmap)
library(ggthemes)
library(plotly)
```

```{r load-data}
accidents <- read_csv("data/ACCIDENT.csv") %>%
  clean_names()

locations <- read_csv("data/ACCIDENT_LOCATION.csv") %>%
  clean_names()

nodes <- read_csv("data/NODE.csv") %>%
  clean_names()

persons <- read_csv("data/PERSON.csv") %>%
  clean_names()

vehicles <- read_csv("data/VEHICLE.csv") %>%
  clean_names()
```

```{r create-year-hour-day}
accidents <- accidents %>%
  mutate(accidentdate = dmy(accidentdate),
         Year = year(accidentdate),
         Hour = hour(accidenttime),
         Weekday = wday(accidentdate, 
                        label = TRUE,
                        abbr = FALSE))
```

Part 3 {data-icon="fa-battery-three-quarters"}
===================================== 

Row {.tabset data-height=800}
---------

### **Accidents Map**

```{r accidents-map}
location_box <- c(min(nodes$long), 
                  min(nodes$lat), 
                  max(nodes$long),
                  max(nodes$lat))

location_map <- get_map(location = location_box, 
                        source = 'osm')

  ggmap(location_map) + 
    geom_point(data = nodes,
               aes(x = long,
                   y = lat), 
               colour="#0072B2", 
               alpha=0.5, 
               size = 0.05) +
    theme_map()
```

### **Accidents by Gender**

```{r accidents-gender-table}
persons <- persons %>%
  mutate(sex = recode(sex,
                      "F" = "Female",
                      "M" = "Male",
                      "U" = "Unknown"))
```

```{r accidents-gender-plot}
persons %>% 
  filter(road_user_type_desc == "Drivers",
         sex %in% c("Female",
                    "Male"),
         age > 15) %>% 
  count(age,
        sex,
        name = "Accidents") %>% 
  
  ggplot(aes(x = age,
             y = Accidents, 
             color = sex)) +
  geom_line() +
  xlab("Age")
  
ggplotly()
```

### **Death Rate by User Type**

```{r user-death-rate}
user_type <- persons %>% 
  count(road_user_type_desc, 
        name = "User")

user_type_fatal <- persons %>% 
  filter(inj_level_desc == "Fatality") %>% 
  count(road_user_type_desc, 
        name = "Fatal")

user_type %>% 
  left_join(user_type_fatal) %>% 
  mutate(death_rate = Fatal / User) %>% 
  
  ggplot(aes(x = fct_reorder(road_user_type_desc, 
                             death_rate), 
             y = death_rate)) +
  geom_col(fill = "#999999") +
  labs(x = "User Type",
       y = "Death Rate") +
  theme(axis.text.x = element_text(angle = 45,
                                   hjust = 1))
  
```

### **Roads with most accidents and deaths**

```{r join-road-names}
roads_accidents <- accidents %>%
  left_join(locations) %>% 
  mutate(Road = paste(road_name,
                      road_type))
```

```{r accidents-by-road}
accidents_per_road <- roads_accidents %>%
  count(Road, 
        name = "Accidents"
        ,sort = T)

deaths_per_road <- roads_accidents %>%
  group_by(Road) %>%
  tally(no_persons_killed,
        name = "Deaths",
        sort = TRUE)

accidents_per_road %>% 
  head(8) %>% 
  kable(caption = "Accidents by road") %>% 
  kable_styling(bootstrap_options = "striped")
```

```{r deadliest-road}
deadliest_road <- accidents_per_road %>%
  left_join(deaths_per_road) %>%
  filter(Accidents >= 100) %>%
  mutate(Deaths_per_accident = Deaths/Accidents) %>%
  arrange(desc(Deaths_per_accident))

deadliest_road %>% 
  head(8) %>% 
  kable(caption = "Deadliest roads") %>% 
  kable_styling(bootstrap_options = "striped")
```